﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_infomation_info_detail : AdminBasePage
{
    private static readonly string TAG_LAYERIDS = OgilvyOne.Utility.ConfigUtil.GetStringValue("ArticleTagLayerids");
    private static readonly string NEWS_LAYERIDS = "1,2,3,25,110;1,2,3,25,216;1,2,3,25,112";
    protected string moreshtml = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.infoname.Text = "新增内容";
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Layerid);

            int columnid = int.Parse(Request.Params["columnid"]);
            ColumnModel column = new ColumnBll().GetColumn(columnid);

            this.isstaticpub.Value = column.Isstaticpub.ToString();
            

            //初始模板
            ModelManageBLL mmb = new ModelManageBLL();
            DataSet temdata = mmb.GetModel("");
            for (int i = 0; i < temdata.Tables.Count; i++)
            {
                DataTable table = temdata.Tables[i];
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    DataRow row = table.Rows[j];

                    this.templateId.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));
                    
                }
            }

            ColumnBll cbll = new ColumnBll();
            IList ilist = cbll.GetColumnList(133);
            for (int i = 0; i < ilist.Count; i++)
            {
                ColumnModel coluModel = (ColumnModel)ilist[i];
                this.buslist.Items.Add(new ListItem(coluModel.Classname, coluModel.Classid.ToString()));
            }

                //初始为栏目设置的模板
            this.templateId.Text = column.Templatedetail.ToString();

            

            if (isExist(Layerid, TAG_LAYERIDS))
            {
                this.contag.Visible = true;
                this.Tr_brands.Visible = true;
                this.Tr_products.Visible = true;
            }
            //1,2,3,18,99

            if (isExist(Layerid, "1,2,3,18,99"))
            {
                this.consubsidiary.Visible = true;
            }
            if (isExist(Layerid, NEWS_LAYERIDS))
            {//副标题
                this.Tr_subtitle.Visible = true;
                this.Tr_subtitle2.Visible = true;
            }
            if(isExist(Layerid, "1,2,3,18,99;1,2,3,133"))
            {//联系信息 - 下属单位
                
                this.Tr_brands.Visible = true;
                this.Tr_concact.Visible = true;
                this.Tr_products.Visible = true;
            }
           /* if (isExist(layerid, "1,2,3,18,99"))
            {
                this.Tr_concact.Visible = true;
            }*/
            this.sub3.Visible = false;
            this.sub4.Visible = false;
            this.sub5.Visible = false;
            CmsArticleBll cabll = new CmsArticleBll();
            if (Request.Params["id"] != null && Request.Params["id"] != "")
            {//修改
                this.infoname.Text = "修改内容";
                CmsArticleModel cm = cabll.GetCmsArticleModel(int.Parse(Request.Params["id"]));
                this.title.Text = cm.Title;
                this.summary.Text = cm.Summary;
                this.content.Text = cm.Content;
                this.indextop.Text = cm.Indextop;
                this.sorttop.Text = cm.Sorttop;
                this.titleImg.Value = cm.Titleimg;
                this.state.Value = cm.Docstate;
                this.aid.Value = cm.Articleid.ToString();
                this.templateId.Text = cm.Templateid.ToString();
                this.isstaticpub.Value = cm.Isstaticpub;
                this.articletype.Text = cm.Articletype;
                if (cm.Articletype == "2")
                {
                    this.con.Visible = false;
                    this.link.Visible = true;
                    this.down.Visible = false;
                    this.linkpath.Text = cm.Linkpath;
                }
                else if(cm.Articletype == "3")
                {
                    this.con.Visible = false;
                    this.link.Visible = false;
                    this.down.Visible = true;
                    this.filedown.Value = cm.Linkpath;
                }
                this.source.Text = cm.Source;
                this.sourceurl.Text = cm.Sourceurl;
                this.linkpath.Text = cm.Linkpath;
                this.author.Text = cm.Author;
                this.ordernum.Value = cm.OrderNum.ToString();
                if (cm.Starttime != null && cm.Starttime.Year>1900)
                {
                    this.startDate.Text = cm.Starttime.ToShortDateString();
                }
                if (cm.ExpertId > 0)
                {
                    this.expertid.Value = cm.ExpertId.ToString();
                    this.expertlist.Text = cm.ExpertId.ToString();
                }
                //需加对应TAG NAME
                this.tag.Value = cm.Tag;
                if (cm.Tag != null && cm.Tag != "")
                {
                    IList taglist = new CmsArticleTagBll().GetTagList(cm.Tag);
                    for (int i = 0; i < taglist.Count; i++)
                    {
                        CmsArticleTagModel catm = (CmsArticleTagModel)taglist[i];
                        this.tagname.Text += catm.TagName + ",";
                    }
                }
                this.sub3.Visible = true;


                //this.buslist.SelectedValue=
              



                //mores
                if (cm.Mores != null)
                {
                    this.subtitle.Text = cm.Mores.Subtitle;
                    this.subtitle2.Text = cm.Mores.Subtitle2;
                    
                    
                    this.concacts.Value = cm.Mores.Concactinfo;

                    if (cm.Mores.Concactinfo != null && cm.Mores.Concactinfo != "")
                    {
                        string[] aids = cm.Mores.Concactinfo.Split(',');
                        foreach (string said in aids)
                        {
                            if (said != null && said != "")
                            {
                                try
                                {
                                    CmsArticleModel _cm = cabll.GetCmsArticleModel(int.Parse(said));
                                    if (_cm != null) this.concact.Text = this.concact.Text + _cm.Title + ",";
                                }
                                catch { }
                            }
                        }
                    }

                    
                    if (cm.Mores.Mores != null && cm.Mores.Mores != "")
                    {
                        /*XmlDocument moresdoc = cm.Mores.GetMoresXml();
                        XmlNode node = moresdoc.SelectSingleNode("//cmsmore");
                        foreach (XmlNode cnode in node.ChildNodes)
                        {
                            string pname = cnode.Name;
                            string con = cnode.InnerText;
                            moreshtml += GetMoreTr(pname, con);//"<tr><td align=\"right\">" + pname + "：</td><td></td><td><textarea name=\"_cmsinfomore_" + pname + "\" rows=\"7\" cols=\"50\" id=\"_cmsinfomore_" + pname + "\">" + con + "</textarea></td></tr>";
                        }*/

                        IList cmores = new CmsColumnMoresBll().GetColumnMoresList(Layerid);
                        for (int i = 0; i < cmores.Count; i++)
                        {
                            CmsColumnMoresModel ccmm = (CmsColumnMoresModel)cmores[i];
                            moreshtml += GetMoreTr(ccmm, cm.Mores.GetMoresInfo(ccmm.Mores_ename));
                        }
                    }
                    else
                    {
                        Moresinit();
                    }
                }
                else
                {
                    Moresinit();
                }

                if (column.Isauditing == 1)
                {
                    if (cm.Docstate != "2") sub3.Visible = false;
                    if (this.PopedomValidateHtml(Request.Params["layerid"], AUDIT))
                    {
                        if (cm.Docstate == "0")
                        {//正稿可以审核
                            this.sub4.Visible = true;
                            this.sub5.Visible = true;
                        } 
                    }
                    if (Request.Params["isaudit"] != null && Request.Params["isaudit"] == "1")
                    {//审核文章
                        this.sub1.Visible = false;
                        this.sub2.Visible = false;
                    }
                }

            }
            else
            {
                if (isExist(Layerid, NEWS_LAYERIDS))
                {
                    this.source.Text = "集团办公厅";
                }
                this.ordernum.Value = (cabll.MaxOrder(columnid) + 1).ToString();
                Moresinit();
            }

            
            
        }
    }

    private void Moresinit()
    {
        IList cmores = new CmsColumnMoresBll().GetColumnMoresList(Layerid);
        for (int i = 0; i < cmores.Count; i++)
        {
            CmsColumnMoresModel ccmm = (CmsColumnMoresModel)cmores[i];
            moreshtml += GetMoreTr(ccmm, "");
        }
    }

    private string GetMoreTr(CmsColumnMoresModel ccmm, string con)
    {
        string trhtml = "<tr><td align=\"right\">" + ccmm.Mores_name + "：</td><td>&nbsp;</td><td>";
        string pid = "_cmsinfomore_" + ccmm.Mores_ename;
        if (ccmm.Mores_type == "textarea")
        {
            trhtml += "<textarea name=\"" + pid + "\" rows=\"" + ccmm.Rows + "\" cols=\"" + ccmm.Cols + "\" id=\"" + pid + "\" maxlength=\"" + ccmm.Maxlength + "\">" + con + "</textarea>\n";
            if(ccmm.Rows>1) trhtml += "<script>$(function (){ initTextarea('"+pid+"')});</script>";
        }
        else if (ccmm.Mores_type == "text")
        {
            trhtml += "<input type=\"text\" name=\"" + pid + "\" id=\"" + pid + "\" value=\"" + con + "\" size=\"50\" maxlength=\"" + ccmm.Maxlength + "\" />";
        }
        else if (ccmm.Mores_type == "date")
        {
            trhtml += "<input type=\"text\" onkeydown=\"return false;\" name=\"" + pid + "\" id=\"" + pid + "\" value=\"" + con + "\" size=\"15\" onClick=\"fPopCalendar(" + pid + "," + pid + ");return false\" />";
        }
        else if (ccmm.Mores_type == "number")
        {
            trhtml += "<input type=\"text\" name=\"" + pid + "\" id=\"" + pid + "\" value=\"" + con + "\" size=\"50\" maxlength=\"" + ccmm.Maxlength + "\" /> (必须是数字)";
        }
        else
        {
            trhtml += "<input type=\"text\" name=\"" + pid + "\" id=\"" + pid + "\" value=\"" + con + "\" size=\"50\" maxlength=\"" + ccmm.Maxlength + "\" />";
        }
        return trhtml + "</td></tr>";
    }

    protected void Audi_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], AUDIT);
        new CmsArticleBll().Audit(int.Parse(this.aid.Value), this.state.Value, GetSessionUsersModel().Username);
        Response.Redirect("info_list.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        int aid = 0;
        if (this.aid.Value!=null && this.aid.Value != "")
        {
            aid = int.Parse(this.aid.Value);
        }

        CmsArticleModel cm = new CmsArticleModel();
        cm.Title = this.title.Text;
        cm.Summary = this.summary.Text;
        cm.Content = this.content.Text;
        cm.Indextop = this.indextop.Text;
        cm.Sorttop = this.sorttop.Text;
        cm.Titleimg = this.titleImg.Value; //相关图片 维护
        cm.Docstate = this.state.Value;
        cm.Templateid = int.Parse(this.templateId.Text);
        cm.Isstaticpub = this.isstaticpub.Value;

        

        cm.Articletype = this.articletype.Text;
        cm.Source = this.source.Text;
        cm.Sourceurl = this.sourceurl.Text;
        cm.OrderNum = int.Parse(this.ordernum.Value);
        if (this.startDate.Text == "")
        {
            cm.Starttime = DateTime.Now;
        }
        else
        {
            DateTime sdate = DateTime.Parse(this.startDate.Text);
            /*if (sdate.CompareTo(DateTime.Now) > -1)
            {
                sdate = DateTime.Now;
            }*/
            cm.Starttime = sdate;
        }
        if (this.articletype.Text == "3")
        {//文件
            cm.Linkpath = this.filedown.Value;
        }
        else
        {//链接
            cm.Linkpath = this.linkpath.Text;
        }
        cm.Author = this.author.Text;
        cm.Tag = this.tag.Value;
        
        if (this.expertid.Value != null && this.expertid.Value != "")
        {
            cm.ExpertId = int.Parse(this.expertid.Value);
        }

        cm.Columnid = int.Parse(Request.Params["columnid"]);

        cm.Addusername = this.GetSessionUsersModel().Username;
        cm.Modifyusername = this.GetSessionUsersModel().Username;


        //start 更多信息 11/09/09 cdy
        string concactinfo = this.concacts.Value;
        string brands = this.brands.Value;
        string products = this.products.Value;
        string subtitle = this.subtitle.Text;
        string subtitle2 = this.subtitle2.Text;
        string subsidiary = this.subsidiary.Value;
        if (Request.Params["layerid"] == "1,2,3,24,104")
        {
            subsidiary = this.buslist.Text;
        }
        IList morelist = new ArrayList();
        foreach (string key in Request.Params.AllKeys)
        {//mores xml
            if (key.IndexOf("_cmsinfomore_") > -1)
            {
                string con = Request.Params[key];
                if (con != null && con != "")
                {
                    string pname = key.Replace("_cmsinfomore_", "");
                    Hashtable m = new Hashtable();
                    m.Add("key", pname);
                    m.Add("con", con);
                    morelist.Add(m);
                }
            }
        }
        string mores = "";
        if (morelist.Count > 0)
        {//
            mores = "<cmsmore>";
            for (int i = 0; i < morelist.Count; i++)
            {
                Hashtable m = (Hashtable)morelist[i];
                string con = (string)m["con"];
                string pname = (string)m["key"];
                mores += "<" + pname + "><![CDATA[" + con + "]]></" + pname + ">";
            }
            mores += "</cmsmore>";
        }


        if ((concactinfo != null && concactinfo != "") ||
            (brands != null && brands != "") ||
            (products != null && products != "") ||
            (mores != null && mores != "") ||
            (subtitle !=null && subtitle!="") ||
            (subtitle2 !=null && subtitle2!="")||
            (subsidiary!=null&&subsidiary!=""))
        {
            CmsArticleMore moremodel = new CmsArticleMore();
            moremodel.Brands = brands;
            moremodel.Products = products;
            moremodel.Concactinfo = concactinfo;
            moremodel.Mores = mores;
            moremodel.Subtitle = subtitle;
            moremodel.Subtitle2 = subtitle2;
            
            cm.Mores = moremodel;
        }

        //end

        if (aid > 0)
        {//修改
            this.PopedomValidate(Request.Params["layerid"], MOD);
            cm.Articleid = aid;
            CmsArticleBll cabll= new CmsArticleBll();
            bool r = cabll.Update(cm);
            if (r)
            {
                insertLog("修改文章:" + this.title.Text);
                if (this.state.Value == "1")
                {
                    cabll.Pubone(aid, GetSessionUsersModel().Username);
                    insertLog("发布文章:" + this.title.Text);
                }
            }
        }
        else
        {
            this.PopedomValidate(Request.Params["layerid"], ADD);
            if (new CmsArticleBll().Add(cm))
            {
                insertLog("新增文章:" + this.title.Text);
            }
        }
        Response.Redirect("info_list.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);

    }

    protected void autotag_onclick(object sender, EventArgs e)
    {
        if (this.content.Text != "")
        {
            this.tagname.Text = "";
            this.tag.Value = "";
            CmsArticleTagModel q = new CmsArticleTagModel();
            q.State = 1;
            IList taglist = new CmsArticleTagBll().GetTagList(q);
            for (int i = 0; i < taglist.Count; i++)
            {
                CmsArticleTagModel catm = (CmsArticleTagModel)taglist[i];
                if (this.content.Text.IndexOf(catm.TagName) > -1)
                {
                    this.tag.Value += catm.TagId + ",";
                    this.tagname.Text += catm.TagName + ",";
                }
            }
        }
        
    }


    protected void articletype_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList rbl = (RadioButtonList)sender;
        this.down.Visible = false;
        this.link.Visible = false;
        this.con.Visible = false;
        if (rbl.Text == "3")
        {//文件
            this.down.Visible = true;
        }
        else if (rbl.Text == "2")
        {//链接
            this.link.Visible = true;

        }
        else
        {
            this.con.Visible = true;
        }
    }
    protected void expertlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.expertlist.SelectedItem.Text != "请选择")
            this.author.Text = this.expertlist.SelectedItem.Text;
        else
            this.author.Text = "";
        
        this.expertid.Value = this.expertlist.SelectedValue;
    }
}
